<?php
class archtmlService extends Service{
    protected $url_service;
	public function __construct(){
		parent::__construct();
		$this->doorway=$this->conf->doorway;
		$this->virtual_pjt=load_pjt(1);
        $this->url_service=load_service('url',P_SVC);
        $this->url_service->set_module_id(11);
        
	}
	public function t(){
		return array('arc','arc_data','attrtaglist','','category');
	}
	//文件写入
	private function _put($file,$data){
		$dir=dirname($file);
		if(!is_dir($dir)) mkdir($dir,0777,true);
		return file_put_contents($file,$data);
	}
	//相当于 xxx.htm.php
	//生成首页
	public function _index(){
		//'static_rule_index'=>array('/index.html',ROOT.'index.html'),
		list($http,$file)=$this->url_service->index();
		
		//$cms_task="html=tohtml_index";
		$cms_task=url('archtml','tohtml_index','',$this->doorway);
		
		ob_start();
		//引入首页类
		$object=load_service('a_index',P_SVC);
		
		//include($this->virtual_pjt['VIEWER_DIR'].'archtml/index.php');
		include(P_VIEWER.'index/index.php');
		$data = ob_get_contents();
		ob_clean();
		$this->_put($file,$data);
		return true;
	}
	//生成列表页
	public function _list($catid,$page_id,$pagesize,$fid,$num){
		//'static_rule_list_index'=>array('/html/{arccat_id}/index.html',ROOT.'html/{arccat_id}/index.html'),
		//'static_rule_list_i'=>array('/html/{arccat_id}/list_{i}.html',ROOT.'html/{arccat_id}/list_{i}.html'),
		
		if(!$catid) return false;
		$CAT=array();
		$query="select * from `{$this->t[4]}` where `id`=:catid limit 1";
		$CAT=$this->m->select($query,array('catid'=>$catid));
		if(!$CAT) return false;
		

        list($http1,$file1)=$this->url_service->var_list_index($catid)->list_index();
		
		
		$condition=" and `status`='1' ";
		$condition.=" and `arccat_id`=:catid";
		
		$page_id=isset($page_id)?max(intval($page_id),1):1;
		
		if($page_id==1){
			$items=$this->m->get_field("select count(*) from `{$this->t[0]}` where 1 {$condition}",array('catid'=>$catid));
			$this->m->query("update `{$this->t[4]}` set `items`='{$items}' where `id`=:catid limit 1",array('catid'=>$catid));
		}else{
			$items=$CAT['items'];
		}
		
		$total=max(ceil($items/$pagesize),1);
		
		if(isset($fid) && isset($num)) {
			$page_id=$fid;
			$topage=$fid+$num-1;
			$total=$topage<$total?$topage:$total;
		}
		//引入列表页类
		$object=load_service('a_list',P_SVC);
		$widget=load_service('a_widget',P_SVC);
		
		for(;$page_id<=$total;$page_id++){
            list($http,$file)=$this->url_service->var_list_i($catid,$page_id)->list_i();

			//$cms_task = "html=tohtml_list&catid=$catid&page_id=$page_id";
			$cms_task=url('archtml','tohtml_list',"catid=$catid&page_id=$page_id",$this->doorway);
			
			$query="select * from `{$this->t[0]}` where 1 {$condition} order by `id` desc";
			$pages=new smart_pager(array($query,array('catid'=>$catid)),$this->m,$pagesize,$page_id,1);
			$pages->url_tpl['filename']='list_%page_id%';
			$pages->url_tpl['extension']='.html';
			
			$info=array();
			if($pages->total_num>0){
				$info=$this->m->mselect($pages->query);
			}
			ob_start();
			//include($this->virtual_pjt['VIEWER_DIR'].'archtml/list.php');
            include template(P_VIEWER.'category/',$CAT['template'],'list.php');
			$data=ob_get_contents();
			ob_clean();
			//unset($pages);
			$this->_put($file,$data);
			if($page_id==1){
				copy($file,$file1);
			}
		}
		return true;
	}
	//生成内容页
	public function _show($id){
		//'static_rule_show'=>array('/html/{cat}/{id}.html',ROOT.'html/{cat}/{id}.html'),
		
		//$cms_task = "html=tohtml_show&id=$id";
		$cms_task=url('archtml','tohtml_show',"id=$id",$this->doorway);
		
		//查询文章信息
		$query="select * from `{$this->t[0]}` where `id`=:id limit 1";
		$item=$this->m->select($query,array('id'=>$id));
		if(!is_array($item) || $item['status']==0) return false;

        $CAT=array();
		$query="select * from `{$this->t[4]}` where `id`=:catid limit 1";
		$CAT=$this->m->select($query,array('catid'=>$item['arccat_id']));
        
		$c=date('Ym',$item['add_time']);

        list($http,$file)=$this->url_service->var_show($c,$id)->show();
		
		$query2="select * from `{$this->t[1]}` where `id`=:id limit 1";
		$item2=$this->m->select($query2,array('id'=>$id));
		
		ob_start();
		
		$object=load_service('a_show',P_SVC);
		$widget=load_service('a_widget',P_SVC);
		
		//include($this->virtual_pjt['VIEWER_DIR'].'archtml/show.php');
        include template(P_VIEWER.'archtml/',$item['template']?$item['template']:$CAT['template'],'show.php');
		$data = ob_get_contents();
		ob_clean();
		$this->_put($file,$data);
		return true;
	}
	//更新地址
	public function _update($id){
		//'static_rule_show'=>array('/html/{cat}/{id}.html',ROOT.'html/{cat}/{id}.html'),
		
		$cms_task='';
		//查询文章信息
		$query="select `add_time`,`htmlurl`,`status` from `{$this->t[0]}` where `id`=:id limit 1";
		$item=$this->m->select($query,array('id'=>$id));
		if(!is_array($item) || $item['status']==0) return false;

		$c=date('Ym',$item['add_time']);

        list($http,$file)=$this->url_service->var_show($c,$id)->show();
        
		if($item['htmlurl']!=$http){
			$query="update `{$this->t[0]}` set `htmlurl`=:http where `id`=:id";
			return $this->m->query($query,array('http'=>$http,'id'=>$id));
		}else{
			return true;
		}
	}
//
}